<template>
	<view class="content">
		<view class="form">
			<view class="titel">
				{{ type == 0 ? '新增外观检测' : '新增尺寸检测' }}
			</view>
			<view class="itembox">
				<view class="name">
					抽检批次号:
				</view>
				<view class="inputBox">
					<input type="number" v-model="info.batchNumber" placeholder="请填写抽检批次号" />
				</view>
			</view>
			<view class="itembox">
				<view class="name" style="font-weight: bold;color: red;">
					样品编号:
				</view>
				<view class="inputBox">
					<input type="number" v-model="info.sampleNumber" placeholder="请填写样品编号" />
				</view>
			</view>
			<view class="itembox">
				<view class="name" style="font-weight: bold;color: red;">
					检验时间:
				</view>
				<view class="inputBox">
					<uni-datetime-picker type="datetime" v-model="info.inspectTime" />
				</view>
			</view>
			<view class="itembox" v-if="type == 0">
				<view class="name">
					外观、表面质量:
				</view>
				<view class="inputBox">
					<input type="text" v-model="info.surfaceQuality" placeholder="请输入外观、表面质量" />
				</view>
			</view>
			<view class="itembox" v-if="type == 1">
				<view class="name">
					尺寸:
				</view>
				<view class="inputBox">
					<input type="text" v-model="info.size" placeholder="请输入尺寸" />
				</view>
			</view>
			<view class="itembox">
				<view class="name">
					是否合格:
				</view>
				<view class="inputBox">
					<!-- <input type="text" v-model="info.testPass" placeholder="请输入外观、表面质量" /> -->
					<uni-data-checkbox v-model="info.testPass" :localdata="select"></uni-data-checkbox>
				</view>
			</view>
			<view class="itembox">
				<view class="name" style="font-weight: bold;color: red;">
					不合格数量:
				</view>
				<view class="inputBox">
					<input type="number" v-model="info.num" placeholder="请输入不合格数量" />
				</view>
			</view>
			<view class="itembox">
				<view class="name">
					备注:
				</view>
				<view class="inputBox">
					<input type="text" v-model="info.remark" placeholder="请输入备注" />
				</view>
			</view>
		</view>
		<view class="bottom-box" v-if="!id">
			<view class="bnt" @click="addBnt()">
				新增
			</view>
		</view>
		<view class="bottom-box" v-if="id">
			<view class="bnt" @click="editBnt()">
				修改
			</view>
		</view>
	</view>
</template>

<script>
// import config from '@/config'
// import {
// 	goodsSaledoGongxuUserMoneyInfo,
// 	addBaogong
// } from "@/api/system/index"
// const baseUrl = config.baseUrl
import * as index from '@/api/index.js'
export default {
	data() {
		return {
			id: '',
			type: '',
			reportWorkId: '',
			info: {
				testPass: 1
			},
			select: [{
				text: '是',
				value: 1
			}, {
				text: '否',
				value: 0
			}],
		}
	},onLoad(e) {
		// 分别处理type和reportWorkId参数，确保即使type不存在，只要reportWorkId存在也能被正确赋值
		if (e.type) {
			this.type = e.type
		}
		if (e.reportWorkId) {
			 e.reportWorkId
		}
		if (e.id) {
			this.id = e.id
			this.reportWorkId = e.id
			this.getInfo()
		} else {
			this.info.inspectTime = Date.now() - 2 * 24 * 3600 * 1000
		}
		// this.deptTreeselect()
		// this.goodsSaledoGongxuUserId()
	},
	methods: {
		getInfo() {
			if (this.type == 0) {
				index.factoryTestSurfaceBuyId({
					id: this.id
				}).then(res => {
					this.info = res.data
					this.info.testPass = parseInt(res.data.testPass)
				})
			} else {
				index.factoryTestSizeBuyId({
					id: this.id
				}).then(res => {
					this.info = res.data
					this.info.testPass = parseInt(res.data.testPass)
				})
			}
		},
		deptName(e) {
			index.getUserByDept({
				deptId: e,
				pageNum: 1,
				pageSize: 100
			}).then(res => {
				this.nameList = res.data.nameList
			})
		},
		goodsSaledoGongxuUserId() {
			index.goodsSaledoGongxuUserId({
				id: this.id
			}).then(res => {
				this.info = res.data
				this.info.doTime = Date.now() - 2 * 24 * 3600 * 1000
				this.selectedWorker[0].selectName = res.data.userName
				this.selectedWorker[0].selectCheName = res.data.deptName
				this.count = res.data.remainCount
				this.info.count = res.data.remainCount
			})
		},
		jianting() {
			var pages = getCurrentPages();
			console.log(pages.length);
			var Page = pages[pages.length - 1]; //当前页
			console.log(pages[pages.length - 1].route); //输出当前路由
			var prevPage = pages[pages.length - 2];
			console.log(pages[pages.length - 2].route); //输出上一个页面的路由
			if (pages[pages.length - 2].route ==
				'pagesA/orderAdmin/qualityTesting/pollingList/index') { //确定要返回到相应页面，在触发
				prevPage.$vm.reload()
			}
		},
		editBnt(e) {
			uni.showModal({
				title: '提示',
				content: '确定修改吗？',
				success: (res) => {
					if (res.confirm) {
						if (this.type == 0) {
							index.factoryTestSurfaceEdit(this.info).then(res => {
								var that = this
								setTimeout(() => {
									var dataForm = {}
									dataForm.iszhijian = '1'
									dataForm.id = this.info.reportWorkId
									index.goodsSaledoGongxuUserMoneyXunJian(dataForm)
										.then(res => {
											setTimeout(() => {
												uni.navigateBack(-1)
												that.jianting()
											}, 100)
										})
										.catch(error => {
											uni.showToast({
												title: '操作失败，请稍后重试',
												icon: 'none'
											});
											console.error('更新巡检状态失败:', error);
										})
								}, 100)
							})
							.catch(error => {
								uni.showToast({
									title: '修改失败，请稍后重试',
									icon: 'none'
								});
								console.error('修改外观检测失败:', error);
							})
						} else {
							index.factoryTestSizeEdit(this.info).then(res => {
								var that = this
								setTimeout(() => {
									var dataForm = {}
									dataForm.iszhijian = '1'
									dataForm.id = this.info.reportWorkId
									index.goodsSaledoGongxuUserMoneyXunJian(dataForm)
										.then(res => {
											setTimeout(() => {
												uni.navigateBack(-1)
												that.jianting()
											}, 100)
										})
										.catch(error => {
											uni.showToast({
												title: '操作失败，请稍后重试',
												icon: 'none'
											});
											console.error('更新巡检状态失败:', error);
										})
								}, 100)
							})
							.catch(error => {
								uni.showToast({
									title: '修改失败，请稍后重试',
									icon: 'none'
								});
								console.error('修改尺寸检测失败:', error);
							})
						}
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		addBnt(e) {
			// 检查reportWorkId是否有值
			if (!this.reportWorkId) {
				uni.showToast({
					title: '缺少必要参数，请返回重试',
					icon: 'none'
				});
				return;
			}
			uni.showModal({
				title: '提示',
				content: '确定添加吗？',
				success: (res) => {
					if (res.confirm) {
						if (this.type == 0) {
							var data = {
								testPass: this.info.testPass,
								reportWorkId: this.reportWorkId,
								batchNumber: this.info.batchNumber,
								sampleNumber: this.info.sampleNumber,
								productNumber: this.info.productNumber,
								surfaceQuality: this.info.surfaceQuality,
								inspectTime: this.info.inspectTime,
								remark: this.info.remark
							}
							index.factoryTestSurfaceAdd(data)
								.then(res => {
									var dataForm = {}
									dataForm.iszhijian = '1'
									dataForm.id = this.reportWorkId
									index.goodsSaledoGongxuUserMoneyXunJian(dataForm)
										.then(res => {
											setTimeout(() => {
												uni.navigateBack(-1)
												this.jianting()
											}, 100)
										})
										.catch(error => {
											uni.showToast({
												title: '操作失败，请稍后重试',
												icon: 'none'
											});
											console.error('更新巡检状态失败:', error);
										})
								})
								.catch(error => {
									uni.showToast({
										title: '添加失败，请稍后重试',
										icon: 'none'
									});
									console.error('添加外观检测失败:', error);
								})
						} else {
							var data = {
								testPass: this.info.testPass,
								reportWorkId: this.reportWorkId,
								batchNumber: this.info.batchNumber,
								sampleNumber: this.info.sampleNumber,
								productNumber: this.info.productNumber,
								inspectTime: this.info.inspectTime,
								size: this.info.size,
								remark: this.info.remark
							}
							index.factoryTestSizeAdd(data)
								.then(res => {
									var dataForm = {}
									dataForm.iszhijian = '1'
									dataForm.id = this.reportWorkId
									index.goodsSaledoGongxuUserMoneyXunJian(dataForm)
										.then(res => {
											setTimeout(() => {
												uni.navigateBack(-1)
												this.jianting()
											}, 100)
										})
										.catch(error => {
											uni.showToast({
												title: '操作失败，请稍后重试',
												icon: 'none'
											});
											console.error('更新巡检状态失败:', error);
										})
								})
								.catch(error => {
									uni.showToast({
										title: '添加失败，请稍后重试',
										icon: 'none'
									});
									console.error('添加尺寸检测失败:', error);
								})
						}
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
	}

}
</script>

<style lang="scss">
.content {
	padding-left: 20rpx;
	padding-right: 20rpx;

	.form {
		margin-top: 20rpx;
		padding: 20rpx;
		background-color: #ffffff;
		border-radius: 10rpx;

		.titel {
			padding-top: 30rpx;
			padding-bottom: 30rpx;
			color: #333;
			font-size: 28rpx;
			font-weight: bold;
		}

		.itembox {
			border-top: 1px solid #EDEDED;
			display: flex;
			align-items: center;
			padding-top: 30rpx;
			padding-bottom: 30rpx;

			.name {
				text-align: right;
				width: 160rpx;
				color: #999;
				font-size: 28rpx;
			}

			.inputBox {
				padding-left: 20rpx;
				width: 600rpx;
				color: #333;
				font-size: 28rpx;
			}
		}

		.itemboxs {
			border-bottom: 1px solid #EDEDED;
			display: flex;
			align-items: center;
			padding-top: 30rpx;
			padding-bottom: 30rpx;

			.name {
				text-align: right;
				width: 160rpx;
				color: #999;
				font-size: 28rpx;
			}

			.inputBox {
				padding-left: 20rpx;
				width: 600rpx;
				color: #333;
				font-size: 28rpx;
				display: flex;
				align-items: center;

				.deleteBnt {
					margin-left: auto;
					padding: 4rpx 12rpx;
					height: 42rpx;
					border-radius: 10rpx;
					background: #F5F7FB;
					color: #8295BC;
					font-size: 24rpx;
				}
			}
		}
	}

	.bottom-box {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 750rpx;
		padding: 30rpx;
		padding-bottom: 120rpx;
		height: 120rpx;
		background: #ffffff;
		display: flex;
		justify-content: center;

		.bnt {
			color: #ffffff;
			width: 306rpx;
			height: 88rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 10rpx;
			background: #4D8987;
		}
	}
}

.areasbox-box {
	background-color: #FFFFFF;
	z-index: 99;

	.areasbox {
		width: 750rpx;
		height: 600rpx;

		.picker-views {
			width: 750rpx;
			height: 600rpx;
			margin-top: 20rpx;
		}

		.items {
			margin-left: 3rpx;
			font-size: 26rpx;
			text-overflow: ellipsis;
			overflow: hidden;
			white-space: nowrap;
			line-height: 100rpx;
			text-align: center;
		}

	}

	.bom-bnt {
		width: 750rpx;
		display: flex;
		align-items: center;
		height: 100rpx;
		background-color: #FFFFFF;
		border-top: 1rpx solid #EAEAEA;

		.cancel {
			width: 50%;
			font-size: 32rpx;
			color: #333;
			text-align: center;
			line-height: 100rpx;
			border-right: 1rpx solid #EAEAEA;
		}

		.addbnt {
			width: 50%;
			text-align: center;
			line-height: 100rpx;
			color: #4D8987;
			font-size: 32rpx;
			font-weight: bold;
		}
	}
}

.add-worker-btn {
	margin-top: 20rpx;
	padding: 20rpx;
	background-color: #f0f0f0;
	border-radius: 10rpx;
	text-align: center;
	color: #333;
	font-size: 28rpx;
}
</style>